<!-- Copyright 2015 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// -->
<!DOCTYPE html>
<title>Unit Test of e2e.openpgp.ContextService</title>
<script src="../../../../../javascript/closure/base.js"></script>
<script src="test_js_deps-runfiles.js"></script>
<script>
  goog.require('e2e.async.Peer');
  goog.require('e2e.async.Client');
  goog.require('e2e.openpgp.ContextService');
  goog.require('goog.testing.AsyncTestCase');
  goog.require('goog.testing.jsunit');
</script>
<script>
var asyncTestCase = goog.testing.AsyncTestCase.createAndInstall(document.title);
var fakeNetwork;
var servicePeer;
var clientPeer;
var FUNCTION_NAME = 'changeKeyRingPassphrase';
var SERVICE_NAME = e2e.openpgp.ContextService.NAME;

function setUp() {
  // Set up a messaging channel between clientPeer and servicePeer.
  fakeNetwork = new MessageChannel();
  servicePeer = new e2e.async.Peer();
  servicePeer.addPort(fakeNetwork.port1);
  clientPeer = new e2e.async.Peer();
  clientPeer.addPort(fakeNetwork.port2);
  clientPeer.init = servicePeer.init = goog.nullFunction;
}


function tearDown() {
}

function testFunctionCalled() {
  var NEW_PASS = 'a new pass';
  var called = false;
  var fakeContext = {};
  fakeContext[FUNCTION_NAME] = function(newPass) {
      assertEquals(NEW_PASS, newPass);
      called = true;
  };

  var contextPromise = new e2e.async.Result.toResult(fakeContext);

  e2e.openpgp.ContextService.launch(servicePeer, contextPromise);

  asyncTestCase.waitForAsync('Waiting for findService()');
  clientPeer.findService(SERVICE_NAME, {}).addCallback(function(response) {
    var res = response.response;
    var port = response.port;
    assertEquals(res.name, e2e.openpgp.ContextService.NAME);
    client = new e2e.async.Client(port);
    asyncTestCase.waitForAsync('Waiting for remote RPC');
    client.deferredCall(FUNCTION_NAME, [NEW_PASS]).addCallback(function() {
      assertTrue(called);
      asyncTestCase.continueTesting();
    }).addErrback(fail);
  });
}

function testFunctionImmediateResult() {
  var returnValue = [1, 2];
  var fakeContext = {};
  fakeContext[FUNCTION_NAME] = function() {
      return returnValue;
  };

  var contextPromise = new e2e.async.Result.toResult(fakeContext);

  e2e.openpgp.ContextService.launch(servicePeer, contextPromise);

  asyncTestCase.waitForAsync('Waiting for findService()');
  clientPeer.findService(SERVICE_NAME, {}).addCallback(function(response) {
    var res = response.response;
    var port = response.port;
    assertEquals(res.name, e2e.openpgp.ContextService.NAME);
    client = new e2e.async.Client(port);
    asyncTestCase.waitForAsync('Waiting for remote RPC');
    client.deferredCall(FUNCTION_NAME, []).addCallback(function(ret) {
      assertArrayEquals(ret, returnValue);
      asyncTestCase.continueTesting();
    }).addErrback(fail);
  });
}

function testFunctionDeferredResult() {
  var returnValue = [1, 2];
  var fakeContext = {};
  fakeContext[FUNCTION_NAME] = function() {
      var result = new e2e.async.Result();
      asyncTestCase.timeout(function() {
        result.callback(returnValue);
      }, 50);
      return result;
  };
  var contextPromise = new e2e.async.Result.toResult(fakeContext);
  e2e.openpgp.ContextService.launch(servicePeer, contextPromise);

  asyncTestCase.waitForAsync('Waiting for findService()');
  clientPeer.findService(SERVICE_NAME, {}).addCallback(function(response) {
    var res = response.response;
    var port = response.port;
    assertEquals(res.name, e2e.openpgp.ContextService.NAME);
    client = new e2e.async.Client(port);
    asyncTestCase.waitForAsync('Waiting for remote RPC');
    client.deferredCall(FUNCTION_NAME, []).addCallback(function(ret) {
      assertArrayEquals(ret, returnValue);
      asyncTestCase.continueTesting();
    }).addErrback(fail);
  });
}

function testFunctionErrors() {
  var errorMsg = 'ERROR!';
  var fakeContext = {};
  fakeContext[FUNCTION_NAME] = function() {
      var result = new e2e.async.Result();
      asyncTestCase.timeout(function() {
        result.errback(errorMsg);
      }, 50);
      return result;
  };
  var contextPromise = new e2e.async.Result.toResult(fakeContext);
  e2e.openpgp.ContextService.launch(servicePeer, contextPromise);

  asyncTestCase.waitForAsync('Waiting for findService()');
  clientPeer.findService(SERVICE_NAME, {}).addCallback(function(response) {
    var res = response.response;
    var port = response.port;
    assertEquals(res.name, e2e.openpgp.ContextService.NAME);
    client = new e2e.async.Client(port);
    asyncTestCase.waitForAsync('Waiting for error in remote RPC');
    client.deferredCall(FUNCTION_NAME, []).addCallbacks(fail, function(error) {
      assertEquals(errorMsg, error);
      fakeContext[FUNCTION_NAME] = function() {
        throw errorMsg;
      }
      asyncTestCase.waitForAsync('Waiting for another error in remote RPC');
      client.deferredCall(FUNCTION_NAME, []).addCallbacks(fail, function(err) {
        assertEquals(errorMsg, err);
        asyncTestCase.waitForAsync(
            'Waiting for nonexistand function remote RPC');
        client.deferredCall('nonexisting', []).addCallbacks(fail,
            function(err) {
          assertEquals('Tried to call nonexistent method.', err);
          asyncTestCase.continueTesting();
        })
      });
    });
  });
}


function testDeferredContextInitialization() {
  var returnValue = 'a result';
  var contextResolved = false;
  var fakeContext = {};
  fakeContext[FUNCTION_NAME] = function() {
    return returnValue;
  };
  var contextPromise = new e2e.async.Result();
  e2e.openpgp.ContextService.launch(servicePeer, contextPromise);
  asyncTestCase.timeout(function() {
    // Delayed initialization of the context
    contextResolved = true;
    contextPromise.callback(fakeContext);
  }, 500);
  asyncTestCase.waitForAsync('Waiting for findService()');
  clientPeer.findService(SERVICE_NAME, {}).addCallback(function(response) {
    var res = response.response;
    var port = response.port;
    assertEquals(res.name, e2e.openpgp.ContextService.NAME);
    assertTrue(contextResolved);
    client = new e2e.async.Client(port);
    asyncTestCase.waitForAsync('Waiting for RPC');
    client.deferredCall(FUNCTION_NAME, []).addCallbacks(function(result) {
      assertEquals(returnValue, result);
      asyncTestCase.continueTesting();
    }, fail);
  });
};
</script>
